home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Talking Clock Pro™ 2.0.1 / Talking Clock Pro Source / Controller / Source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-24  |  4.9 KB  |  226 lines  |  [TEXT/CWIE]

  1. /*
  2.  * main.c
  3.  */
  4.  
  5. #include "window.h"
  6. #include "apprec.h"
  7. #include "clockrec.h"
  8. #include "x.h"
  9. #include "str.h"
  10. #include "menu.h"
  11. #include "util.h"
  12. #include "TalkConstants.h"
  13.  
  14.  
  15. /* This will be used as a breathing hole for the error reporting routine */
  16. static Handle err_safeguard = NULL ;
  17.  
  18. void InstallAEHandlers ( void ) ;
  19.  
  20.  
  21.  
  22. static void
  23. InitMac ( void ) {
  24.  
  25.     MaxApplZone ( ) ;
  26.     MoreMasters ( ) ;
  27.  
  28.     InitGraf ( & qd . thePort ) ;
  29.     InitFonts ( ) ;
  30.     InitWindows ( ) ;
  31.     InitMenus ( ) ;
  32.     TEInit ( ) ;
  33.     InitDialogs ( NULL ) ;
  34.  
  35.     InitCursor ( ) ;
  36. }
  37.  
  38.  
  39. static void
  40. InitApp ( DefWindowRec * theApp ) {
  41.  
  42. FSSpec prefFile ;
  43. short vRefNum ;
  44. long parID ;
  45. Str63 name ;
  46.  
  47.     gMouse = NewRgn ( ) ;
  48.     FailNil ( gMouse ) ;
  49.     CopyRgn ( GetGrayRgn ( ) , gMouse ) ;
  50.     gDragLimit = ( * GetGrayRgn ( ) ) -> rgnBBox ;
  51.     InsetRect ( & gDragLimit , 3 , 3 ) ;
  52.     gSizeLimit = gDragLimit ;
  53.     OffsetRect ( & gSizeLimit , - gSizeLimit . left , - gSizeLimit . top ) ;
  54.     gSizeLimit . top = 100 ;
  55.     gSizeLimit . left = 150 ;
  56.     gSizeLimit . bottom -= GetMBarHeight ( ) ;
  57.     if ( gSizeLimit . right > 512 ) {
  58.         gSizeLimit . right -= 32 ;
  59.     }
  60.  
  61.     MakeMenus ( ) ;
  62.     StrGet ( 128 , name , 63 ) ;
  63.     FailErr ( FindFolder ( kOnSystemDisk , kPreferencesFolderType , kCreateFolder ,
  64.         & vRefNum , & parID ) ) ;
  65.     FSMakeFSSpec ( vRefNum , parID , name , & prefFile ) ;
  66.     app = * theApp ;
  67.     ( * app . OnCreate ) ( NULL , & app . data , & prefFile ) ;
  68. }
  69.  
  70.  
  71. static void
  72. QuitApp ( void ) {
  73.  
  74.     ( * app . OnDestroy ) ( NULL , app . data ) ;
  75. }
  76.  
  77.  
  78. static void
  79. ReportError ( short errCode , Boolean toQuit ) {
  80.  
  81. StringHandle h = GetString ( errCode ) ;
  82. Str15 code ;
  83. StringHandle num ;
  84. Str31 fileStr ;
  85. Str15 lineStr ;
  86.  
  87.     DisposeHandle ( err_safeguard ) ;
  88.     if ( ! h ) {
  89.         h = GetString ( 129 ) ; /* What do we do if this fails !? */
  90.     }
  91.     HLock ( ( Handle ) h ) ;
  92.     NumToString ( errCode , code ) ;
  93.     num = GetString ( 130 ) ;
  94.     if ( num ) {
  95.         PtrAndHand ( ( Ptr ) & code [ 1 ] , ( Handle ) num , code [ 0 ] ) ;
  96.         * * num += code [ 0 ] ;
  97.         HLock ( ( Handle ) num ) ;
  98.     }
  99.  
  100. #ifdef FAILINFO
  101.     NumToString ( __err_line , lineStr ) ;
  102.     CopyC2P ( __err_file , fileStr ) ;
  103. #else
  104.     lineStr [ 0 ] = fileStr [ 0 ] = 0 ;
  105. #endif
  106.  
  107.     ParamText ( h ? * h : NULL , num ? * num : NULL , fileStr , lineStr ) ;
  108.     if ( num ) {
  109.         ReleaseResource ( ( Handle ) num ) ;
  110.     }
  111.     if ( h ) {
  112.         ReleaseResource ( ( Handle ) h ) ;
  113.     }
  114.     if ( ! InFront ( ) ) {
  115.         Notify ( ) ;
  116.     }
  117.     Alert ( 128 , NULL ) ;
  118.     HiliteMenu ( 0L ) ; /* For failures during menu selections */
  119.     ParamText ( NULL , NULL , NULL , NULL ) ;
  120.     err_safeguard = NewHandle ( 1024 ) ;
  121. }
  122.  
  123.  
  124. static void
  125. CheckVersions ( void ) {
  126.  
  127. #if 1
  128. /* These are the typical System-7 checks (we assume System 6 which means
  129.  * WaitNextEvent is always implemented etc.)
  130.  */
  131. Boolean fine = 1 ;
  132.  
  133.     fine = fine && CheckGestaltBit ( gestaltFindFolderAttr , gestaltFindFolderPresent ) ;
  134.     fine = fine && CheckGestaltBit ( gestaltAppleEventsAttr , gestaltAppleEventsPresent ) ;
  135.     fine = fine && CheckGestaltBit ( gestaltAliasMgrAttr , gestaltAliasMgrPresent ) ;
  136.     fine = fine && CheckGestaltBit ( gestaltOSAttr , gestaltLaunchControl ) ;
  137.     fine = fine && CheckGestaltBit ( gestaltOSAttr , gestaltRealTempMemory ) ;
  138.     fine = fine && CheckGestaltBit ( gestaltOSAttr , gestaltTempMemTracked ) ;
  139. /*    We test at each location we call Color QuickDraw, I hope */
  140. //    fine = fine && CheckGestaltBit ( gestaltQuickdrawFeatures , gestaltHasColor ) ;
  141. //    fine = fine && CheckGestaltBit ( gestaltQuickdrawFeatures , gestaltHasDeepGWorlds ) ;
  142.     fine = fine && SpeechAvailable ( ) ;
  143.     if ( ! fine ) {
  144.         Alert ( 131 , NULL ) ;
  145.         ExitToShell ( ) ;
  146.     }
  147. #endif
  148. }
  149.  
  150.  
  151. void
  152. main ( ) {
  153.  
  154. WindowPtr wp ;
  155.  
  156.     InitMac ( ) ;
  157.     CheckVersions ( ) ;
  158.     err_safeguard = NewHandle ( 1024 ) ;
  159.     InstallAEHandlers ( ) ;
  160.     TRY {
  161.         InitApp ( & appRec ) ;
  162.     runAgain :
  163.         while ( ! quit ) {
  164.             TRY {
  165.                 DriveWindow ( ) ;
  166.                 if ( InFront ( ) ) {
  167.                     DeNotify ( ) ;
  168.                 }
  169.             } CATCH {
  170.                 ReportError ( __err , 0 ) ;
  171.                 NO_PROPAGATE ;
  172.             } DONE ;
  173.         }
  174.         AppSaveWindows ( app . data ) ;
  175.         while ( (wp = FrontWindow()) != NULL ) {
  176.         short err ;
  177.             if ( (err = DestroyWindow(wp)) != noErr ) {
  178.                 quit = 0 ;
  179.                 ReportError ( err , 0 ) ;
  180.                 goto runAgain ;
  181.             }
  182.         }
  183.         QuitApp ( ) ;
  184.     } CATCH {
  185.         ReportError ( __err , 1 ) ;
  186.         NO_PROPAGATE ;
  187.     } DONE ;
  188. }
  189.  
  190.  
  191. static pascal OSErr
  192. AEOpenApplication ( const AppleEvent * event , AppleEvent * reply , long refCon ) {
  193.  
  194. ProcessSerialNumber psn ;
  195. extern Boolean hasController ;
  196.  
  197.     if ( FrontWindow ( ) ) {
  198.         if ( FindPSN ( 'FNDR' , 'MACS' , & psn ) ) {
  199.             FailErr ( SetFrontProcess ( & psn ) ) ;
  200.         }
  201.     } else if ( ! hasController ) {
  202.     WindowPtr wp = MakeWindow ( NULL , & clockRec ) ;
  203.         SelectWindow ( wp ) ;
  204.         ShowWindow ( wp ) ;
  205.     }
  206.     return noErr ;
  207. }
  208.  
  209.  
  210. static pascal OSErr
  211. AEQuitApplication ( const AppleEvent * event , AppleEvent * reply , long refCon ) {
  212.  
  213.     quit = 1 ;
  214.     return noErr ;
  215. }
  216.  
  217.  
  218. static void
  219. InstallAEHandlers ( void ) {
  220.  
  221.     FailErr ( AEInstallEventHandler ( kCoreEventClass , kAEOpenApplication ,
  222.         AEOpenApplication , 0L , 0 ) ) ;
  223.     FailErr ( AEInstallEventHandler ( kCoreEventClass , kAEQuitApplication ,
  224.         AEQuitApplication , 0L , 0 ) ) ;
  225. }
  226.